package com.ponhu.ea2.goodsCenter.provider.service.impl;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.ponhu.ea2.entity.PhModel;
import com.ponhu.ea2.enums.ResponseCodeEnum;
import com.ponhu.ea2.goodsCenter.provider.mapper.PhModelMapper;
import com.ponhu.ea2.goodsCenter.provider.service.PhModelService;
import com.ponhu.ea2.goodsCenter.provider.utils.LocalDateUtil;
import com.ponhu.ea2.support.exception.BusinessRuntimeException;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 型号表  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-10-18
 */
@Service
public class PhModelServiceImpl
        extends ServiceImpl<PhModelMapper, PhModel>
        implements PhModelService {

    @Resource
    PhModelMapper phModelMapper;

    /**
     * 根据品牌或系列查询所有型号
     *
     * @return
     */
    @Override
    public List<PhModel> getPhModelsBySeries(Long seriesId) {

        LambdaQueryWrapper<PhModel> queryWrapper = Wrappers.lambdaQuery(PhModel.class);
        if (ObjectUtils.isNotEmpty(seriesId)) {
            queryWrapper.eq(PhModel::getSeriesId, seriesId);
        }

        return list(queryWrapper);
    }

    /**
     * 根据品牌查询所有的型号
     *
     * @param brandId
     * @return
     */
    @Override
    public List<PhModel> getPhmodelsByBrandId(Long brandId) {
        if(ObjectUtils.isEmpty(brandId)){
            return Collections.EMPTY_LIST;
        }
        LambdaQueryWrapper<PhModel> queryWrapper = Wrappers.lambdaQuery(PhModel.class);
        queryWrapper.eq(PhModel::getBrandId, brandId);
        return list(queryWrapper);
    }

    /**
     * 根据多品牌查询所有的型号
     */
    @Override
    public List<PhModel> getListByBrandId(List<Long> brandIds) {
        if (CollectionUtils.isEmpty(brandIds)) return Lists.newArrayList();

        LambdaQueryWrapper<PhModel> queryWrapper = Wrappers
                .lambdaQuery(PhModel.class).in(PhModel::getBrandId, brandIds);

        return list(queryWrapper);
    }

    @Override
    public PhModel getPhModelBySerialIdAndName(Long seriesId, String modelName) {
        return this.getOne(Wrappers.lambdaQuery(PhModel.class)
                .eq(PhModel::getSeriesId, seriesId)
                .eq(PhModel::getModel, modelName).last("limit 1"));
    }

    @Override
    public PhModel updateModel(String model, Long modelId, Long seriesId, Long branddId) {
        PhModel phModel = new PhModel();
        if (modelId != null) {
            phModel.setId(modelId).setModel(model);
            if (!updateById(phModel)) {
                throw new BusinessRuntimeException(ResponseCodeEnum
                        .MODEL_EDIT_EXISTS_800902);
            }
        } else {
            phModel = checkPhModelByName(model, seriesId, branddId);
        }
        return phModel;
    }

    @Override
    public PhModel checkPhModelByName(String model, Long seriesId, Long branddId) {
        if (StringUtils.isBlank(model)) {
            return new PhModel();
        }
        PhModel phModel = phModelMapper.getModelByName(model.toLowerCase()
                .replaceAll(" ", "")
                .replaceAll("_", "")
                .replaceAll("-", ""), branddId,seriesId);
        if (ObjectUtils.isEmpty(phModel)) {
            return createPhmodel(model, seriesId, branddId);
        }
        return phModel;
    }

    private PhModel createPhmodel(String model, Long seriesId, Long brandId) {
        PhModel phModel = new PhModel().setBrandId(brandId)
                .setSeriesId(seriesId)
                .setModel(model)
                .setCreatedTime(LocalDateUtil.dateToLocalDateTime(new Date()));
        if (!save(phModel)) {
            throw new BusinessRuntimeException(ResponseCodeEnum
                    .MODEL_SAVE_EXISTS_800901);
        }
        return phModel;
    }

}
